package com.openlogic.flow.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.collections4.CollectionUtils;
import org.dromara.warm.flow.core.utils.StreamUtils;
import org.dromara.warm.flow.orm.entity.FlowTask;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.validation.annotation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mybatisflex.core.paginate.Page;
import com.openlogic.common.core.enums.BusinessType;
import com.openlogic.common.core.web.controller.BaseController;
import com.openlogic.common.core.web.domain.PageData;
import com.openlogic.common.core.web.domain.entity.SysRole;
import com.openlogic.common.core.web.domain.entity.SysUser;
import com.openlogic.common.core.web.domain.model.LoginUser;
import com.openlogic.common.datasource.annotation.QzLog;
import com.openlogic.common.security.utils.SecurityUtil;
import com.openlogic.flow.domain.query.FlowTaskQuery;
import com.openlogic.flow.domain.vo.FlowTaskVo;
import com.openlogic.flow.service.IFlowCopyService;

/**
 * @ClassName: FlowCopyController
 * @Description: 流程抄送 控制层
 * @author weiyongpeng
 * @date 2025年2月22日 上午9:08:22
 */
@Valid
@Controller
@Mapping("flow/copy")
public class FlowCopyController extends BaseController {
	
	private static final Logger log = LoggerFactory.getLogger(FlowCopyController.class);

	@Inject
	IFlowCopyService flowCopyService;
	
   /**
	* 描述：抄送列表
	* @Title: copyPage
	* @param startFlowTaskDto
	* @return
	* @author weiyongpeng
	* @date  2025年2月22日 上午9:09:43
	*/
	@QzLog(title = "抄送列表", businessType = BusinessType.LIST)
	@Get
	@Mapping("copyPage")
	public PageData<FlowTaskVo> copyPage(Page<FlowTask> page, FlowTaskQuery flowTaskQuery ) {
		LoginUser sysUser = SecurityUtil.getLoginUser();
		List<String> permissionList = permissionList(String.valueOf(sysUser.getUserid()), sysUser.getSysUser().getDept().getDeptId(), sysUser.getSysUser());
		flowTaskQuery.setPermissionList(permissionList);
		startPage(page);
        List<FlowTaskVo> list = flowCopyService.copyPage(flowTaskQuery);
        return getPageData(list);
	}
	
    /**
     * 获取权限
     * @param userId  用户编号
     * @param deptId  部门编号
     * @param sysUser 登陆用户
     * @return 权限列表
     */
    private List<String> permissionList(String userId, Long deptId, SysUser sysUser) {
        List<SysRole> roles = sysUser.getRoles();
        List<String> permissionList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(roles)) {
            permissionList = StreamUtils.toList(roles, role -> "role:" + role.getRoleId());
        }
        permissionList.add(userId);
        if (Objects.nonNull(deptId)) {
            permissionList.add("dept:" + deptId);
        }
        log.info("当前用户所有权限[{}]", permissionList);
        return permissionList;
    }

}
